//
//  SMLeftViewController.m
//  SaveMoney
//
//  Created by user on 2018/11/26.
//  Copyright © 2018年 Sunflower. All rights reserved.
//

#import "SMLeftViewController.h"

#import "SMLeftTableViewCell.h"
#import "SMLoginViewController.h"
#import "MSNavigationViewController.h"
#import "SMPassWordTableViewController.h"

@interface SMLeftViewController ()<UITableViewDelegate, UITableViewDataSource, changePasswordDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIImageView *touxiangImage;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet SBBaseLabel *smartLabel;

@property (nonatomic, copy) NSArray *listArray;

@end

@implementation SMLeftViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    UIImage *image = [UIImage base64ChangeImageWithEncode:SMUserInfoModel.sharedStorage.avatar];
    [_touxiangImage setImage:image];
    
    _nameLabel.text = [SMUserInfoModel.sharedStorage.nickName isValid]? SMUserInfoModel.sharedStorage.nickName:@"某某某";
    
    
    NSString *beginTele = [SMUserInfoModel.sharedStorage.phone substringWithRange:NSMakeRange(0,3)];
    NSString *tele = [SMUserInfoModel.sharedStorage.phone substringWithRange:NSMakeRange(7, 4)];
    NSString *decTele = [NSString stringWithFormat:@"%@****%@",beginTele,tele];
    _smartLabel.text = decTele;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _listArray = @[@"个人信息",@"存钱管理", @"账号管理", @"数据同步"];
    
    KViewBorderRadius(_touxiangImage, SBScaleNumber(100)/2, 1, [UIColor grayColor]);
}


#pragma mark - UITableViewDelegate, UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _listArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return SBScaleNumber(50);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return SBScaleNumber(90);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SMLeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SMLeftTableViewCell" forIndexPath:indexPath];
    cell.titleLabel.text = _listArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if (indexPath.row == 0) {
        UIViewController *vc = [[UIStoryboard storyboardWithName:@"Left" bundle:nil] instantiateViewControllerWithIdentifier:@"SMAccountViewController"];
        [self.navigationController pushViewController:vc animated:YES];
    } else if (indexPath.row == 1) {
        UIViewController *vc = [[UIStoryboard storyboardWithName:@"Left" bundle:nil] instantiateViewControllerWithIdentifier:@"SMSaveMoneyListViewController"];
        [self.navigationController pushViewController:vc animated:YES];
    } else if (indexPath.row == 2) {
        SMPassWordTableViewController *vc = [[UIStoryboard storyboardWithName:@"Left" bundle:nil] instantiateViewControllerWithIdentifier:@"SMPassWordTableViewController"];
        vc.delegate = self;
        [self.navigationController pushViewController:vc animated:YES];
    } else if (indexPath.row == 3) {
        NSString *json = [SMDataManage sharedStorage].upJson;  
        [SVProgressHUD show];
        [[NetworkAction sharedAction] upLoadDataInfoWithPhone:[SMUserInfoModel sharedStorage].phone jsonData:json success:^(id model, NSString *message) {
            [SVProgressHUD showSuccessWithStatus:message];
        } failuer:^(NSError *error) {
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        }];
        
    } else {
        
    }
    
    
}



- (void)changePassword {
    [self outLogin:nil];
}


- (IBAction)dismiss:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)outLogin:(id)sender {
    __weak typeof(self) weakSelf = self;
    [self dismissViewControllerAnimated:NO completion:^{
        [weakSelf.temVC dismissViewControllerAnimated:YES completion:^{
            [[SMUserInfoModel sharedStorage] clear];
            [[SMDataManage sharedStorage] removeData];
            MSNavigationViewController *account = [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateInitialViewController];
            [UIApplication sharedApplication].keyWindow.rootViewController =  account;
            [[UIApplication sharedApplication].keyWindow makeKeyAndVisible];
        }]; 
    }];
    
     
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@""]) {
        
    }
}


@end
